<?php

/**
 * 开放商户 - 账单下载查询功能使用示例
 */

require_once __DIR__ . '/../../../vendor/autoload.php';

use JLPay\Core\OrgConfig;
use JLPay\Core\DemoConfig;
use JLPay\Services\Openmerch\Bill\BillService;
use JLPay\Models\Openmerch\Bill\BillDownloadQueryRequest;
use JLPay\Utils\Logger;
use JLPay\Utils\DemoRequestHandler;

// =================== 日志配置 =====================
$logger = Logger::getInstance();

// =================== 工具函数 =====================
function createConfig(): OrgConfig
{
    return DemoConfig::createConfig();
}

// =================== 数据构建函数 =====================
function buildBillDownloadQueryRequest(): BillDownloadQueryRequest
{
    $request = new BillDownloadQueryRequest();
    
    // 设置请求参数
    $request->setBillType("1")  // 1-日交易账单
            ->setDt("20230401");  // 账单日期yyyyMMdd
    
    return $request;
}

// =================== 示例函数 =====================
function billDownloadQueryDemo(): void
{
    global $logger;

    $logger->info("开始执行账单下载查询示例");
    
    // 创建配置对象和账单服务
    $config = createConfig();
    $billService = new BillService($config);
    
    // 创建账单下载查询请求
    $request = buildBillDownloadQueryRequest();
    
    // 发送请求 - 使用DemoRequestHandler
    $response = DemoRequestHandler::sendRequestAndHandleResponse(
        $billService, 
        $request, 
        "账单下载查询",
        function($service, $request) {
            return $service->queryBillDownload($request);
        }
    );
    
    // 处理响应结果
    if ($response && $response->isSuccess()) {
        $logger->info("响应报文：" . json_encode($response->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
    }
    
    $logger->info("账单下载查询示例执行完毕");
}

// =================== 主程序 =====================
$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 账单下载查询示例 开始 =====");
$logger->info("=" . str_repeat("=", 50));

billDownloadQueryDemo();

$logger->info("=" . str_repeat("=", 50));
$logger->info("===== 账单下载查询示例执行完毕 =====");
$logger->info("=" . str_repeat("=", 50)); 